package com.portal.incurridos.persistence.dao.impl;

import javax.annotation.Resource;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.stereotype.Component;

import com.portal.incurridos.persistence.dao.CheckListDao;
import com.portal.incurridos.persistence.enumeration.StateTransactionEnum;
import com.portal.incurridos.persistence.wall.CheckList;

/**
 * @author Alan Dávila
 * e-mail ustargab@gmail.com
 * date 24/11/2013 23:47:32
 */
@Component("checkListDaoImpl")
public class CheckListDaoHibernateImpl implements CheckListDao {

	/**
	 * Managment bean of dao layer.
	 */
	@Resource(name = "sessionFactory")
    private SessionFactory factoria;

	/* (non-Javadoc)
	 * @see com.portal.incurridos.persistence.dao.CheckListDao#get(java.lang.Long)
	 */
	public final CheckList get(final Long checkList) {
		CheckList checkListOut = null;
		Session sesion = null;
		try {
			sesion = factoria.getCurrentSession();
			checkListOut = (CheckList) sesion.get(CheckList.class, checkList);
			sesion.close();
		} catch (Exception e) {
			checkListOut = null;
		}
		return checkListOut;
	}

	/* (non-Javadoc)
	 * @see com.portal.incurridos.persistence.dao.CheckListDao#create(com.portal.incurridos.persistence.wall.CheckList)
	 */
	public final CheckList create(final CheckList checkList) {
		Session sesion = null;
		Transaction tx = null;
		CheckList checkListOut = checkList;
		try {
			sesion = factoria.getCurrentSession();
			tx = sesion.beginTransaction();
			sesion.save(checkListOut);
			tx.commit();
			sesion.close();
		} catch (Exception e) {
			checkListOut = null;
		}
		return checkListOut;
	}

	/* (non-Javadoc)
	 * @see com.portal.incurridos.persistence.dao.CheckListDao#update(com.portal.incurridos.persistence.wall.CheckList)
	 */
	public final StateTransactionEnum update(final CheckList checkList) {
		Session sesion = null;
		Transaction tx = null;
		try {
			sesion = factoria.getCurrentSession();
			tx = sesion.beginTransaction();
			sesion.update(checkList);
			tx.commit();
			sesion.close();
		} catch (Exception e) {
			return StateTransactionEnum.ERROR;
		}
		return StateTransactionEnum.UPDATED;
	}

	/* (non-Javadoc)
	 * @see com.portal.incurridos.persistence.dao.CheckListDao#delete(com.portal.incurridos.persistence.wall.CheckList)
	 */
	public final StateTransactionEnum delete(final CheckList checkList) {
		Session sesion = null;
		Transaction tx = null;
		try {
			sesion = factoria.getCurrentSession();
			tx = sesion.beginTransaction();
			sesion.delete(checkList);
			tx.commit();
			sesion.close();
		} catch (Exception e) {
			return StateTransactionEnum.ERROR;
		}
		return StateTransactionEnum.DELETED;
	}

}
